define(['zepto', 'butterfly/view','../../bsl',
    'text!./accelerometer.html'
], function($, View, bsl, accelerometer) {
    var me = null;

    var view = View.extend({
        aid : null,
        events: {
 			"tap #getAcc":"getAcc",
 			"tap #watchAcc":"watchAcc",
 			"tap #watchStop":"watchStop",
 			"tap #back" : "back",
            "tap #clicked" : "clicked",
            "tap #openDoc" : "OpenDoc",
        },
        render: function() {
            me = this;
        },

        onShow: function() {
      
        },
        getAcc: function() {
            console.info("获取设备当前加速度");

            bsl.accelerometer.getCurrentAcceleration(function(a) {
                console.info("X轴：" + a.xAxis + "\nY轴：" + a.yAxis + "\nZ轴：" + a.zAxis);
            }, function(e) {
                console.info("获取失败:" + e.message);
            });
        },
        

        watchAcc: function() {
            if (this.aid) {
                return;
            }
            console.info("监听设备加速度变化");
            this.aid = bsl.accelerometer.watchAcceleration(function(a) {
                console.info("监听设备加速度变化\n" + "X轴：" + a.xAxis + "\nY轴：" + a.yAxis + "\nZ轴：" + a.zAxis)
            }, function(e) {
                console.info("监听失败:" + e.message);
            });
        },

        watchStop: function() {

            if (this.aid) {
                console.info("停止监听设备加速度变化");
                bsl.accelerometer.clearWatch(this.aid);
                this.aid = null;
            } else {
                console.info("没有监听设备加速度变化");
            }
        },
        back : function(e){
            window.history.go(-1);
        },
        clicked : function(e){
            // alert("dd")
            window.butterfly.navigate("com.no5.cordovademo/page/accelerometer_shake", {
                trigger: true
            });
        },
        OpenDoc : function(e){
           
            window.butterfly.navigate('com.no5.cordovademo/doc/accelerometer', {
                trigger: true
            });
        },

    });
    return view;
});
